package com.iaiai.tm.activity;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.app.ActivityManager;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;

import com.iaiai.tm.activity.viewpagerindicator.TitlePageIndicatorEx1;
import com.iaiai.tm.activity.viewpagerindicator.TitleProvider;

public class MainActivity extends FragmentActivity {

	private List<View> mListViews;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		TestPagerAdapter ad = new TestPagerAdapter();
		ViewPager pager = (ViewPager) findViewById(R.id.pager);
		pager.setAdapter(ad);

		TitlePageIndicatorEx1 indicator = (TitlePageIndicatorEx1) findViewById(R.id.indicator);
		indicator.setViewPager(pager);
		indicator.setFooterIndicatorStyle(TitlePageIndicatorEx1.IndicatorStyle.Underline);

		mListViews = new ArrayList<View>();
		LayoutInflater flater = getLayoutInflater();
		mListViews.add(flater.inflate(R.layout.application, null));
		mListViews.add(flater.inflate(R.layout.process, null));
		mListViews.add(flater.inflate(R.layout.service, null));
//		mListViews.add(flater.inflate(R.layout.performance, null));
	}

	private List<String> getData() {
		List<String> list = new ArrayList<String>();

		ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
		List l = am.getRunningAppProcesses();
		Iterator i = l.iterator();
		PackageManager pm = this.getPackageManager();
		while (i.hasNext()) {
			ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo) (i.next());
			try {
				CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName,
						PackageManager.GET_META_DATA));
				list.add(c.toString());
			} catch (Exception e) {
			}
		}

		return list;
	}

	class TestPagerAdapter extends PagerAdapter implements TitleProvider {

		@Override
		public void destroyItem(View arg0, int arg1, Object arg2) {
			((ViewPager) arg0).removeView(mListViews.get(arg1));
		}

		@Override
		public void finishUpdate(View arg0) {
		}

		@Override
		public int getCount() {
			return mListViews.size();
		}

		@Override
		public Object instantiateItem(View arg0, int arg1) {
			((ViewPager) arg0).addView(mListViews.get(arg1));
			return mListViews.get(arg1);
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			// return false;
			return arg0 == arg1;
		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1) {
		}

		@Override
		public Parcelable saveState() {
			return null;
		}

		@Override
		public void startUpdate(View arg0) {
		}

		public String getTitle(int position) {
			switch (position) {
			case 0:
				return "应用程序";
			case 1:
				return "进程";
			case 2:
				return "服务";
			case 3:
				return "性能";
			case 4:
				return "联网";
			case 5:
				return "用户";
			}
			return null;
		}

	}
	
	@Override  
    public boolean onCreateOptionsMenu(Menu menu) {  
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		return true;
    }
	
	@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.menu_performance:
        	Intent it = new Intent(this, PerformanceActivity.class);  
        	startActivityForResult(it, 0);
            break;
        case R.id.menu_about:
        	new AlertDialog.Builder(this).setTitle("关于")
			.setPositiveButton(android.R.string.ok, null)
			.setMessage("任务管理器 - 丸子\r\n\r\n版本:1.1\r\nQQ:176291935")
			.show();
            break;
        default:
            break;
        }
        return super.onOptionsItemSelected(item);
    }

}